package com.ship.dispatch.controller;

import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageInfo;
import com.ship.common.core.base.Page;
import com.ship.common.core.domain.R;
import com.ship.common.core.exception.BusException;
import com.ship.common.core.exception.ServiceException;
import com.ship.common.core.utils.FileUtils;
import com.ship.common.core.web.domain.BaseResultCode;
import com.ship.common.core.web.domain.JsonResult;
import com.ship.common.security.utils.SecurityUtils;
import com.ship.dispatch.bean.boats.ExportDetailRq;
import com.ship.dispatch.bean.boats.info.QueryMonthlyCountInfo;
import com.ship.dispatch.bean.boats.info.QueryPortEntranceInfo;
import com.ship.dispatch.bean.boats.info.QueryPortInfo;
import com.ship.dispatch.bean.boats.info.QueryVolumeTradeInfo;
import com.ship.dispatch.bean.fob.SpFobFileRecord;
import com.ship.dispatch.bean.performance.PerformanceList;
import com.ship.dispatch.bean.performance.vo.PerformanceExp;
import com.ship.dispatch.bean.performance.vo.PerformanceHis;
import com.ship.dispatch.bean.performance.vo.PerformanceListVo;
import com.ship.dispatch.service.ExportService;
import com.ship.dispatch.service.HomePageService;
import com.ship.dispatch.service.PerformanceListService;
import com.ship.dispatch.service.SpOverhaulService;
import com.ship.dispatch.service.fob.SpFobFileRecordService;
import com.ship.dispatch.vo.SpOverhaulVo;
import com.ship.system.api.RemoteFileService;
import com.ship.system.api.domain.SysFile;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/spPerformance")
@Api(value = "performance", tags = "船舶性能前端控制器")
public class PerformanceController {
    private static final Logger logger = LoggerFactory.getLogger(PerformanceController.class);

    @Autowired
    PerformanceListService performanceListService;

    @Autowired
    private ExportService exportService;

    @Autowired
    private SpFobFileRecordService spFobFileRecordService;

    @Autowired
    private RemoteFileService remoteFileService;

    @Autowired
    private HomePageService homePageService;

    @ApiOperation("查询船舶名称列表")
    @PostMapping("/query/shipName/list")
    public JsonResult queryShipNameList(){
        List<String> shipNameList = performanceListService.queryShipNameList();
        return JsonResult.success(shipNameList);
    }

    /**
     * 船舶性能列表
     * @param performanceListVo
     * @return
     */
    @ApiOperation(value = "船舶性能列表")
    @PostMapping("/page")
    public JsonResult getSchemeDetailPage(@RequestBody PerformanceListVo performanceListVo) {
        JsonResult<PageInfo<PerformanceList>> pageinfo = performanceListService.getListInfo(performanceListVo);
        return pageinfo;
    }

    @ApiOperation("导出船舶性能列表")
    @PostMapping("/page/export")
    public JsonResult SchemeDetailPageExport(@RequestBody ExportDetailRq rq){
        List<PerformanceList> performanceList = performanceListService.SchemeDetailPageExport(rq.getIds());
        Field[] fields = PerformanceList.class.getDeclaredFields();
        List<String> list = exportService.listStrByModuleAndUser
                ("sp_performance_message", SecurityUtils.getUserId());
        logger.info("获取参数:{}"+JSONUtil.parse(list));
        String url = exportService.pageExport(performanceList,fields,
                exportService.listStrByModuleAndUser
                        ("sp_performance_message", SecurityUtils.getUserId()),
                "船舶性能记录");
        return JsonResult.success(url);
    }

    /**
     * 船舶性能基础信息
     * @param id
     * @return
     */
    @ApiOperation(value = "船舶性能基础信息")
    @GetMapping("/info")
    public JsonResult getInfo(@RequestParam("id") String id) {
        return performanceListService.getInfo(id);
    }

    /**
     * 船舶租期
     * @param imo
     * @return
     */
    @ApiOperation(value = "船舶租期")
    @GetMapping("/tenancy")
    public JsonResult getTenancyInfo(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                     @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                                     @RequestParam("imo") String imo) {
        return performanceListService.getTenancyInfo(pageNum,pageSize,imo);
    }

    /**
     * 历史航次
     * @param performanceHis
     * @return
     */
    @ApiOperation(value = "历史航次")
    @PostMapping("/history")
    public JsonResult getHistoryInfo(@RequestBody PerformanceHis performanceHis) {
        return performanceListService.getHistoryInfo(performanceHis);
    }

    /**
     * 报文
     * @param performanceHis
     * @return
     */
    @ApiOperation(value = "报文")
    @PostMapping("/report")
    public JsonResult getReportInfo(@RequestBody PerformanceHis performanceHis) {
        return performanceListService.getReportInfo(performanceHis);
    }

    /**
     * 船舶检修记录
     * @param imo
     * @return
     */
    @ApiOperation(value = "船舶检修记录")
    @GetMapping("/overhaul")
    public JsonResult getOverhaulInfo(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                      @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                                      @RequestParam("imo") String imo) {
        return performanceListService.getOverhaulInfo(pageNum,pageSize,imo);
    }

    /**
     * 导出
     * @param performanceExp
     * @return
     */
    @PostMapping("/export")
    @ApiOperation(value = "导出")
    public JsonResult<Object> export(@RequestBody PerformanceExp performanceExp){
        String url = performanceListService.export(performanceExp);
        return JsonResult.success(url);
    }

    @ApiOperation("上传船舶性能油耗信息")
    @PostMapping("/upload/shipConsumption")
    public JsonResult uploadShipConsumption(@RequestParam("file") MultipartFile file,@RequestParam Long id){
        FileUtils.checkFileName(file);
        String name = file.getOriginalFilename();
        R<SysFile> result1 = remoteFileService.upload(file);
        if(result1.getCode() != R.SUCCESS){
            throw new BusException("文件服务器连接失败", BaseResultCode.GENERAL_ERROR);
        }
        String url = result1.getData().getHttpsUrl();
        SpFobFileRecord spFobFileRecord = new SpFobFileRecord();
        spFobFileRecord.setPath(url);
        spFobFileRecord.setName(name);
        spFobFileRecord.setFileType("船舶油耗");
        spFobFileRecord.setShipPropertyId(id);
        spFobFileRecord.setCreateDate(new Date());
        spFobFileRecord.setCreateBy(SecurityUtils.getStringUserId());
        spFobFileRecordService.save(spFobFileRecord);
        return JsonResult.success(spFobFileRecord.getId().toString());
    }

    @ApiOperation("查询所有上传油耗信息")
    @GetMapping("/query/shipConsumption/all")
    public JsonResult queryShipConsumptionList(@RequestParam Long id){
        List<SpFobFileRecord> fileRecords = spFobFileRecordService.queryShipConsumptionList(id);
        return JsonResult.success(fileRecords);
    }

    @ApiOperation("根据油耗信息附件ID删除附件信息")
    @GetMapping("/delete/shipConsumption/byId")
    public JsonResult deleteShipConsumptionById(@RequestParam Long id){
        boolean status = spFobFileRecordService.deleteShipConsumptionById(id);
        return JsonResult.success(status);
    }

}
